<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>商家注册列表</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form">
            <div style="float: left; margin-left: 15px; margin-top: 20px;">
                <div class="demoTable">
                    <div class="layui-inline">
                        <label class="layui-form-label">商户名称：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="nickName" placeholder="请输入"
                                   autocomplete="off" class="layui-input"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <!--审批状态选择搜索框-->
                        <label class="layui-form-label">审批状态：</label>
                        <div class="layui-input-inline" id="status">
                            <select name="status" lay-search="">
                                <option value="">请选择</option>
                                <option value="0">等待审批</option>
                                <option value="1">审批已通过</option>
                                <option value="2">审批未通过</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <!--搜索按钮-->
            <div class="layui-inline"
                 style="float: left; margin-left: 30px; margin-top: 20px;">
                <button class="layui-btn" lay-submit=""
                        lay-filter="data-search-btn">搜索
                </button>
            </div>

            <!--清空搜索框按钮-->
            <div class="layui-inline"
                 style="float: left; margin-left: 30px; margin-top: 20px;">
                <button type="reset" class="layui-btn">清空</button>
            </div>
        </form>
        <!--表格功能-->
    </div>
</div>
<div style="float: left; margin-left: 10px;">
    <table id="merchantList" class="merchantList" lay-filter="merchantList"></table>
</div>
<script th:src="@{/src/layui.js}"></script>

<script>
    layui.use(['jquery', 'layer', 'laypage', 'laytpl', 'table', 'form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            laypage = layui.laypage,
            laytpl = layui.laytpl,
            form = layui.form,
            table = layui.table;
        table.render({
            id: 'merchantList',
            elem: '#merchantList',
            url: '/merchant/list',
            title: '商家信息列表',
            cellMinWidth: 80,
            page: true,
            limit: 5,
            limits: [5, 10, 20],
            cols: [
                [
                    {type: 'checkbox'},
                    {title: '编号', field: 'id', width: 50},
                    {title: '商户名称', field: 'nickName', width: 100},
                    {title: '小区名称', field: 'areaName', width: 120},
                    {title: '邮箱', field: 'mailBox', width: 150},
                    {title: '注册时间', field: 'createTime', width: 120},
                    {title: '商家地址', field: 'address', width: 120},
                    {title: '商家介绍', field: 'introduce', width: 120},
                    {title: '审批状态', field: 'status', templet: '#statusEdit', width: 120},
                    {title: '操作时间', field: 'operationTime', width: 120},
                    {title: '操作', templet: '#barEdit', width: 180}
                ]
            ],
            parseData: function (result) {
                return {
                    code: result.code == 200 ? 0 : result.code,
                    msg: result.message,
                    count: result.data.total,
                    data: result.data.list
                }
            }
        })
        table.on('tool(merchantList)', function (row) {
            var data = row.data;//获取当前行的数据
            var event = row.event;// 获得 lay-event 对应的值


                if (event === 'examine') {//执行审批
                    layer.confirm('确认审批操作？', function (index) {
                    $.ajax({
                        url: '/merchant/confirm/' + data.id,
                        type: 'put',
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('审批通过', {icon: 1, time: 1000}, function () {
                                    parent.location.reload();
                                });
                            } else {
                                layer.msg(result.message, {icon: 5});
                            }
                        }
                    })
                })
                layer.close(index);
        }else if (event === 'refuse') {
                layer.confirm('确认拒绝该条信息审批？', function (index) {
                    $.ajax({
                        url: '/merchant/refuse/' + data.id,
                        type: 'put',
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('执行成功', {icon: 1, time: 1000}, function () {
                                    parent.location.reload();
                                });
                            } else {
                                layer.msg(result.message, {icon: 5});
                            }
                        }
                    })
                    layer.close(index);
                })
            }
        })
        form.on('submit(data-search-btn)', function (data) {
            var nickName = data.field.nickName;
            var status = data.field.status;

            //执行搜索重载
            table.reload('merchantList', {
                page: {
                    curr: 1
                }
                , where: {
                    nickName: nickName,
                    status: status
                },
                method: 'get',
                url: '/merchant/select',
            });
            return false;
        });
    })
</script>

<script type="text/html" id="barEdit">
    {{# if (d.status==0) { }}
    <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="examine"
       shiro:lacksPermission="sys:ml:examine">确认审批</a>
    <a class="layui-btn layui-btn-xs" lay-event="examine" shiro:hasPermission="sys:ml:examine">确认审批</a>

    <a class="layui-btn layui-btn-xs layui-btn-disabled" lay-event="refuse"
       shiro:lacksPermission="sys:ml:refuse">拒绝审批</a>
    <a class="layui-btn layui-btn-xs" lay-event="refuse" shiro:hasPermission="sys:ml:refuse">拒绝审批</a>
    {{# }else{ }}
    <span>该申请已处理</span>
    {{# } }}
</script>

<script type="text/html" id="statusEdit">
    {{# if (d.status==0) { }}
    <span>等待审批</span>
    {{# }else if(d.status==1){ }}
    <span>审批已通过</span>
    {{# }else { }}
    <span>审批未通过</span>
    {{# } }}
</script>

</body>
</html>